package com.oa.action;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.oa.entity.Forum;
import com.oa.entity.Topic;
import com.oa.service.ForumService;
import com.oa.service.TopicService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@Controller("forumAction")
@Scope("prototype")
public class ForumAction extends ActionSupport implements ModelDriven<Forum>,
		ServletRequestAware {

	private static final long serialVersionUID = 1L;

	private Forum forum;
	private HttpServletRequest req;
	private int type;
	private String queryAllForumUrl;
	private String root;
	private String postingTitle;
	@Resource(name="forumService")
	private ForumService forumService;
	@Resource(name="topicService")
	private TopicService topicService;

	public String getPostingTitle() {
		return postingTitle;
	}

	public void setPostingTitle(String postingTitle) {
		this.postingTitle = postingTitle;
	}

	public String getRoot() {
		return root;
	}

	public void setRoot(String root) {
		this.root = root;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public String getQueryAllForumUrl() {
		return queryAllForumUrl;
	}

	public void setQueryAllForumUrl(String queryAllForumUrl) {
		this.queryAllForumUrl = queryAllForumUrl;
	}

	public void setServletRequest(HttpServletRequest req) {
		this.req = req;

	}

	public Forum getForum() {
		return forum;
	}

	public void setForum(Forum forum) {
		this.forum = forum;
	}

	public Forum getModel() {
		if (forum == null)
			forum = new Forum();
		return forum;
	}

	// 显示版块信息
	public String queryAllForum() {
		List<Forum> forumList = forumService.selectAllForum();
		ActionContext.getContext().put("forumList", forumList);
		if (type == 0)
			queryAllForumUrl = "/jsp/forumAction/forumList.jsp";
		else
			queryAllForumUrl = "/jsp/forumAction/list.jsp";
		return "queryAllForum";
	}

	// 显示版块下的主题信息
	public String showTopic() {
		Forum forumNext = forumService.seleteForum(forum.getForumId());
		List<Topic> topicList = topicService.selectAllTopic(forum.getForumId());
		String showTopicUrl = req.getServletPath() + "?forumId="
				+ forum.getForumId();
		ActionContext.getContext().getSession()
				.put("showTopicUrl", showTopicUrl);
		
		System.out.println(forumNext.getForumId()+">>>>>>>>>>>>>>>>>>>>>>>");
		
		ActionContext.getContext().getSession().put("forumNext", forumNext);
		ActionContext.getContext().put("topicList", topicList);
		return "showTopic";
	}

	// 删除版块
	public String deleteForum() {

		Forum forumNext = forumService.seleteForum(forum.getForumId());
		forumService.deleteForum(forumNext);
		return "deleteForum";
	}

	// 添加版块
	public String addForum() {
		forumService.addForum(forum);
		return "addForum";
	}

	// 修改版块信息
	public String updateForum() {
		Forum forumNext = forumService.seleteForum(forum.getForumId());
		forumNext.setForumDescription(forum.getForumDescription());
		forumNext.setForumName(forum.getForumName());
		forumService.updateForum(forumNext);
		return "updateForum";
	}

	// 显示版块信息

	public String queryForums() {
		List<Forum> forumList = forumService.selectAllForum();
		Topic topic = topicService.selectTopicName(postingTitle);
		ActionContext.getContext().put("topic", topic);
		ActionContext.getContext().put("forumList", forumList);
		return "queryForums";
	}

}
